#include "LoginDlg.h"
#include <wx/wx.h>
#include <wx/xrc/xmlres.h>
#include "AdplatformApp.h"
#include "AdSkin.h"
#include "res/dialog_bk.xpm"

#ifdef _WIN32
#else
#include <unistd.h>
#define Sleep(x)		usleep((x) * 10000)
#endif // _WIN32

CLoginDlg::CLoginDlg(void)
{
	wxImage::AddHandler( new wxPNGHandler );
	wxImage::AddHandler( new wxXPMHandler );
	// 关联资源
	wxXmlResource::Get()->Load(wxT("Login.xrc"));
	wxXmlResource::Get()->LoadDialog(this, NULL, wxT("Login"));
	// 关联事件
	m_staticError = XRCCTRL(*this, "m_staticError", wxStaticText);
	m_textServer = XRCCTRL(*this, "m_textServer", wxTextCtrl);
	m_textUser = XRCCTRL(*this, "m_textUser", wxTextCtrl);
	m_textPwd = XRCCTRL(*this, "m_textPwd", wxTextCtrl);
	m_btnLogin = XRCCTRL(*this, "m_btLogin", AdSkinButton);
//	m_btnLogin->SubclassWin(XRCCTRL(*this, "m_btLogin", wxButton))

	//m_btnLogin->SetImage(wxT("btn.png"), wxBITMAP_TYPE_PNG);
	m_staticError->SetLabel(wxT(""));
	m_textUser->SetFocus();
}

CLoginDlg::~CLoginDlg(void)
{

}

BEGIN_EVENT_TABLE(CLoginDlg, wxDialog)
	EVT_BUTTON(XRCID("m_btCancel"), CLoginDlg::OnCancel)
	EVT_BUTTON(XRCID("m_btLogin"), CLoginDlg::OnLogin)
	EVT_THREAD(CLoginDlg::OnBeginLogin)
	EVT_ERASE_BACKGROUND(CLoginDlg::OnEraseBackground)
END_EVENT_TABLE()

void CLoginDlg::OnCancel( wxCommandEvent &event )
{
	wxDialog::Close();
}

void CLoginDlg::OnLogin( wxCommandEvent &event )
{
	uiQuickThread::CreateThread(this);
}

void* CLoginDlg::ThreadEntry(void* param)
{
	uiThreadEvent		te;

	ProcessEvent(te);
	for(int i = 0; i < 10; i++)
	{
		wxString		str = wxString::Format(wxT("Loging %d"), i);

		Sleep(1000);
		te.m_wParam = (void *)1;
		te.m_lParam = (void *)str.GetData();
		ProcessEvent(te);
	}

	te.m_wParam = (void *)2;
	ProcessEvent(te);

	return NULL;
}

void CLoginDlg::OnBeginLogin( uiThreadEvent &event )
{
	switch((long)event.m_wParam)
	{
	case 0:
		m_btnLogin->Enable(false);
		m_staticError->SetLabel(wxT("Loging"));
		break;
	case 1:
		m_staticError->SetLabel((wxChar*)event.m_lParam);
		break;
	case 2:
		m_btnLogin->Enable(true);
		m_staticError->SetLabel(wxT(""));
		break;
	}
}

void CLoginDlg::OnEraseBackground(wxEraseEvent& rEvent)
{
	wxDC*		dc			= rEvent.GetDC();
	wxImage		img(dialog_bk_xpm);
	wxBrush		brush(img);

//	dc->SetBackground(brush);
	dc->SetPen(*wxTRANSPARENT_PEN);
	dc->SetBrush(brush);
	dc->DrawRectangle(wxRect(0, 0, GetClientRect().GetWidth(), img.GetHeight()));
//	wxDialog::OnEraseBackground(rEvent);
	dc->SetBrush(*wxWHITE_BRUSH);
	dc->DrawRectangle(wxRect(0, img.GetHeight(), GetClientRect().GetWidth(), GetClientRect().GetHeight()));

}
